<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * 经纪人模型
 */
class Agent extends Model
{
    use HasFactory;

    protected $guarded = [];

    protected $table = 'agents';


    public static function getList($params)
    {
        return self::where('name', 'like', '%' . $params . '%')->get();
    }

    //多条件搜索分页
    public static function Getpaginate($params,$limit,$offset){
            DB::enableQueryLog();
            $data['data']=self::when($params,function ($query) use ($params)
            {
                if (isset($params['search']) && !empty($params['search'])) {
                    $query->where('name', 'like', '%' . $params['search'] . '%');
                    $query->orWhere('phone', 'like', '%' . $params['search'] . '%');
                }
                if (isset($params['status']) && !empty($params['status'])) {
                    $query->where('status', '=' . $params['status']);
                }
            })
                ->offset($offset)//offset是偏移量
                ->limit($limit)//limit展示每页条数
                ->get()
                ->toArray();
            //dd($data);
            //dd(DB::getQueryLog());

          $data['total'] =(int)self::when($params, function ($query) use ($params) {
            if (isset($params['name']) && !empty($params['name'])) {
                $query->where('name', 'like', '%' . $params['name'] . '%');
            }
            if (isset($params['phone']) && !empty($params['phone'])) {
                $query->Where('phone', 'like', '%',$params['phone']);
            }
           })->count();//offset是偏移量; //mit展示每页条数;

            $data['limit'] =(int)$limit;
            return $data;
    }
    //查看所有的经纪人
    static function lookBroder()
    {
        return self::where('is_del', '0')->get()->toArray();
    }

    //查找展示
    static function findBorder(array $where)
    {
        return self::where($where)->where('is_del', '0')->get()->toArray();
    }

    //添加经纪人
    static function border(array $params)
    {
        return self::create($params)->toArray();
    }

    //修改经纪人
    static function updateBroker(int $id, array $params)
    {
        return self::where('id', $id)->where('is_del', '0')->update($params);
    }

    //删除经纪人
    static function deleteBroker(int $id)
    {
        return self::where('id', $id)
            ->where('is_del', '0')
            ->update(['is_del' => '1']);
    }

    //经纪人详情
    public static function getOne($id)
    {
        return self::where('id',$id)->where('is_del','0')->get()->toArray();
    }

}
